package com.vonchange.utils.uuid.back;

import java.util.Random;
import java.util.UUID;

import com.fasterxml.uuid.EthernetAddress;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.TimeBasedGenerator;
import com.vonchange.utils.uuid.NumberToStr;

public class UUIDT {
	private static String digits(long val, int digits) {
		long hi = 1L << (digits * 4);
		return NumberToStr.toString(hi | (val & (hi - 1)), NumberToStr.MAX_RADIX)
				.substring(1);
	}
	
	/**
	 * 以62进制（字母加数字）生成19位UUID，最短的UUID
	 * 
	 * @return
	 */
	public static String uuid() {
		UUID uuid = UUID.randomUUID();
		StringBuilder sb = new StringBuilder();
		sb.append(digits(uuid.getMostSignificantBits() >> 32, 8));
		sb.append(digits(uuid.getMostSignificantBits() >> 16, 4));
		sb.append(digits(uuid.getMostSignificantBits(), 4));
		sb.append(digits(uuid.getLeastSignificantBits() >> 48, 4));
		sb.append(digits(uuid.getLeastSignificantBits(), 12));
		return sb.toString();
	}
	final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',  
        '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',  
        'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',  
        'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',  
        'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',  
        'Z' };  
	public static String id(int size) {  
	    Random random = new Random();  
	    char[] cs = new char[size];  
	    for (int i = 0; i < cs.length; i++) {  
	        cs[i] = digits[random.nextInt(digits.length)];  
	    }  
	    return new String(cs);  
	}  
	public static void main(String[] args) {
		
		TimeBasedGenerator gen = Generators.timeBasedGenerator(EthernetAddress.fromInterface());
		UUID uuid = gen.generate();
		UUID anotherUuid = gen.generate();
	    System.out.println(uuid.toString());
	    System.out.println(anotherUuid.toString());
	    String resultString=anotherUuid.toString();
	    String[]  strs=resultString.split("-");
	    String aString=strs[2]+strs[1]+strs[0]+strs[4]+strs[3];
	    System.out.println(aString);
	}
}

